package interfaz;

import org.eclipse.swt.SWT;
import org.uqbar.arena.actions.MessageSend;

import org.uqbar.arena.layout.VerticalLayout;
import org.uqbar.arena.widgets.Button;
import org.uqbar.arena.widgets.Label;
import org.uqbar.arena.widgets.Panel;
import org.uqbar.arena.widgets.TextBox;

import org.uqbar.arena.windows.MainWindow;
import org.uqbar.arena.windows.MessageBox;

import org.uqbar.ui.jface.builder.ErrorsPanel;
import org.uqbar.ui.jface.view.ErrorViewer;

import tablero.Coordenada;
import tablero.Tablero;

public class CrearTableroVentana extends MainWindow<Coordenada> implements
		ErrorViewer {
	private String taskDescription;

	public CrearTableroVentana(Coordenada model) {
		super(model);
		this.setTitle("Crear un tablero");
	}

	public void createContents(Panel mainPanel) {
		this.getDelegate().setErrorViewer(this);

		mainPanel.setLayout(new VerticalLayout());
		mainPanel.setLayoutInColumns(2);

		new Label(mainPanel).setText("Alto");
		new TextBox(mainPanel).bindValueToProperty("y");

		new Label(mainPanel).setText("Ancho");
		new TextBox(mainPanel).bindValueToProperty("x");

		Button nuevoElemento = new Button(mainPanel);
		nuevoElemento.setCaption("Crear tablero");
		nuevoElemento.onClick(new MessageSend(this, "crearTablero"));
		nuevoElemento.setAsDefault();
		Button cancelar = new Button(mainPanel);
		cancelar.setCaption("Cancelar");
		cancelar.onClick(new MessageSend(this, "cancelar"));

	}

	public void cancelar() {

		this.close();

	}

	public int crearTablero() {

		Coordenada c = this.getModel();

		int x = c.getX();
		int y = c.getY();

		if (y < 1 || x < 1) {
			this.showError("Las medidas no pueden ser negativas, ni cero");
			return 0;
		}

		Tablero t = new Tablero(x, y);
		this.close();
		new PrincipalWindow(this, t).open();
		return 0;

	}

	// private int validateCoordenada(int c) {
	// System.out.println(Integer.signum(c) != -1);
	// if(Integer.signum(c) != -1){
	// return c;
	// }
	// else{
	// System.out.println(":S");
	// //throw new UserException("La coordenada no puede ser negativa");
	// this.showError("La coordenada no puede ser negativa");
	// return 0;
	// //No funciona porque MainWindow no tiene propiedades para mostrar los
	// errores como por ejemplo las SimpleWindow
	// }
	// }

	public static void main(String[] args) {
		Coordenada c = new Coordenada();
		CrearTableroVentana b = new CrearTableroVentana(c);
		b.startApplication();
	}

	protected ErrorsPanel createErrorsPanel(Panel mainPanel) {
		return new ErrorsPanel(mainPanel, this.getTaskDescription());
	}

	public String getTaskDescription() {
		return this.taskDescription != null ? this.taskDescription : this
				.getTitle();
	}

	public void setTaskDescription(String taskDescription) {
		this.taskDescription = taskDescription;
	}

	// ********************************************************
	// ** Mensajes (ErrorViewer)
	// ********************************************************

	@Override
	public void showInfo(String message) {
		this.showMessageBox(message, SWT.OK | SWT.ICON_INFORMATION);
	}

	@Override
	public void showWarning(String message) {
		this.showMessageBox(message, SWT.OK | SWT.ICON_WARNING);
	}

	@Override
	public void showError(String message) {
		this.showMessageBox(message, SWT.OK | SWT.ICON_ERROR);
	}

	protected void showMessageBox(String message, int style) {
		MessageBox messageBox = new MessageBox(this, style);
		messageBox.setMessage(message);
		messageBox.open();
	}

}
